<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$module = sxREQ("mod");
$errMsg = "";
if($module==''){
	$errMsg .= _t("mod missing<br>");
}
if(sxREQ("reqtype") == "save"){
	$errMsg .= puIns();
}
$sSQL = "SELECT s.sub_module, s.description FROM sub_module s "
	." WHERE s.module = '$module' ";
$sSQL .= " ORDER BY s.sub_module";
$objSDAO->query($sSQL, "pu");
$aSub = array(''=>'-- select --');
while($row = $objSDAO->fetch("pu")) {
	$aSub[$row['sub_module']] = $row['sub_module']." - ".$row['description'];
}

//output HTML
$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>false);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('mod');

echo getModHeader($module);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save data"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Process-unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Sub-module (*)</td>" .
			"<td class=\"text01\">".getHtmlSelect('sub', $aSub)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name (*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"process_unit\" id=\"process_unit\" maxlength=\"32\" size=\"32\" value=\"$process_unit\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description (*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Type")."</td>" .
			"<td class=\"text01\">");
echo("<select name=\"type\" id=\"type\">");
echo("<option value=\"\">----</option>");
$selWeb = "";
$selBatch = "";
if($type == "web-page-reloadable"){
	$selWebR = "SELECTED";
} elseif($type == "batch"){
	$selBatch = "SELECTED";
} else {
	$selWebN = "SELECTED";
}
echo("<option value=\"web-page-reloadable\" $selWebR>web-page-reloadable</option>");
echo("<option value=\"web-page-unreloadable\" $selWebN>web-page-unreloadable</option>");
echo("<option value=\"batch\" $selBatch>batch</option>");
echo("</select>");
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Document</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"doc_name\" id=\"doc_name\" maxlength=\"80\" size=\"32\" value=\"".sxHE($doc_name)."\">&nbsp;&nbsp;<input class=\"inpt01\" type=\"file\" name=\"document\" id=\"document\"></td>" .
		"</tr>\n");
echo("</table>");
echo $sCmd;	
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

 function puIns($redirOnEnd = true){
 global $objSDAO;
	$errMsg = "";
	if(preg_match("/[^a-zA-Z0-9_]/",sxREQ("process_unit")))
		$errMsg .= "Name: letter, number and _ only<br>";
	if(sxRTrim("process_unit") == "")
		$errMsg .= "missing name<br>";
	if(sxRTrim("sub") == "")
		$errMsg .= "missing sub-module<br>";
	if(sxRTrim("mod") == "")
		$errMsg .= "missing module<br>";
	if(trim($_REQUEST["description"]) == "")
		$errMsg .= "missing description<br>";
	if($errMsg != ""){ return $errMsg; }		
	
	//controllo che il processo non esista gi@
	$module = 	sxREQ("mod");
	$sub_module = 	sxREQ("sub");
	$process_unit = 	"$module.$sub_module.".trim(sxREQUEST("process_unit"));
	$sSQL = "SELECT count(*) as num FROM process_unit WHERE process_unit='".sxAS($process_unit)."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= _t("Il processo")." '$process_unit' "._t("esiste gi&agrave;.");
	if($errMsg != ""){ return $errMsg; }		
	
	$sSQL = "INSERT INTO " .
			"process_unit " .
			"(process_unit, " .
			"description, " .
			"module, " .
			"sub_module, " .
			"type, " .
			"note)" .
			"VALUES('".sxAS($process_unit)."', " .
			"'".trim(sxRAS("description"))."', " .
			"'".sxAS($module)."', " .
			"'".sxAS($sub_module)."', " .
			"'".trim(sxRAS("type"))."', " .
			"'".trim(sxRAS("note"))."')";	
	$objSDAO->exec($sSQL);
	
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','W','','simple memory variable','','N')";
	$objSDAO->exec($sSQL);
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','X','','1 dimension array memory variable','','N')";
	$objSDAO->exec($sSQL);
	$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
			" VALUES('".sxAS($process_unit)."','Y','','2 dimensions array memory variable','','N')";
	$objSDAO->exec($sSQL);
	
	insertDoc($process_unit, $errMsg);
	$url = "puView.php?pu=$process_unit&xsn=".XSN;
	serverRedir($url);
	return 'abnormal end';
}
